package com.sanjiaozhou.gameserver.utils;


import java.io.Serializable;

/**
 *
 * <p>
 * 业务处理结果:
 * </p>
 *
 * @author Liu TianLong 2019年1月10日 下午3:30:49
 */
@SuppressWarnings("serial")
public class BusinessResult<T> implements Serializable,Cloneable {
    private int code;
    private T result;
    private String message;

    public static final class ResultCode {
        public final static int SUCCESS = 0; // 成功
        public final static int FAIL = 1; // 失败
        public final static int INVALID_TOKEN = 20001; // 无效token
    }

    public BusinessResult() {
    }

    public BusinessResult(int code) {
        this.code = code;
    }

    public BusinessResult(int code, T result) {
        this.code = code;
        this.result = result;
    }

    public BusinessResult<T> data(T result) {
        this.result = result;
        return this;
    }

    public BusinessResult<T> message(String message) {
        this.message = message;
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return this.code == ResultCode.SUCCESS;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public static <T> BusinessResult<T> success() {
        return new BusinessResult<T>(BusinessResult.ResultCode.SUCCESS);
    }

    public static <T> BusinessResult<T> success(T result) {
        return new BusinessResult<T>(BusinessResult.ResultCode.SUCCESS).data(result);
    }

    public static <T> BusinessResult<T> fail() {
        return new BusinessResult<T>(BusinessResult.ResultCode.FAIL);
    }

    public static <T> BusinessResult<T> fail(String message) {
        return new BusinessResult<T>(BusinessResult.ResultCode.FAIL).message(message);
    }

    public static <T> BusinessResult<T> fail(int code, String message) {
        return new BusinessResult<T>(code).message(message);
    }
}
